package com.sd.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;

import com.alex.log.ALog;
import com.sd.util.HtmlFinish;
import com.sd.util.WebUtil;
import com.sd.view.ToastManager;
import com.sd.view.ViewUtil;

/**
 * 
 * 
 * @author Alex.Lu
 * 
 */
public class CHUCardRechargeActivity extends BaseActivity implements OnClickListener, HtmlFinish {

	private ImageButton mReturn;
	private Button mCardTypeBT;
	private EditText mAmountET;
	private EditText mCardValueET;
	private EditText mCardNoET;
	private EditText mPasswordET;
	private Button mRechargeBT;
	private LinearLayout mWarmTipLL;
	private String[] strings;
	private String[] values;
	private int mCardType;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_recharge_chu);

		LinearLayout accountLL = (LinearLayout)findViewById(R.id.accountLL);
		accountLL.addView(ViewUtil.getAccountView(this, "account", "value"));
		
		mCardTypeBT = (Button)findViewById(R.id.card_type);
		mCardTypeBT.setOnClickListener(this);
		mAmountET = (EditText)findViewById(R.id.amount);
		mCardValueET = (EditText)findViewById(R.id.card_value);
		mCardNoET = (EditText)findViewById(R.id.card_no);
		mPasswordET = (EditText)findViewById(R.id.card_password);
		
		mRechargeBT = (Button)findViewById(R.id.recharge);
		mRechargeBT.setOnClickListener(this);
		
		mWarmTipLL = (LinearLayout)findViewById(R.id.warmtipLL);
		mWarmTipLL.addView(ViewUtil.getWarmTips(this, R.string.warmtip1));
		
		mReturn = (ImageButton) findViewById(R.id.return_btn);
		mReturn.setOnClickListener(this);
		mCardType = 0;
		strings = this.getResources().getStringArray(R.array.recharge_type);
		values = this.getResources().getStringArray(R.array.recharge_value);
		mCardTypeBT.setText(strings[mCardType]);
		mCardTypeBT.setTag(values[mCardType]);
	}

	@Override
	protected Dialog onCreateDialog(int id) {
		// TODO Auto-generated method stub
		
		return new AlertDialog.Builder(CHUCardRechargeActivity.this)
        .setTitle(R.string.hint_card_type)
        .setSingleChoiceItems(R.array.recharge_type, 0, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            	mCardType = whichButton;
            }
        })
        .setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
            	mCardTypeBT.setText(strings[mCardType]);
            	mCardTypeBT.setTag(values[mCardType]);
                /* User clicked Yes so do some stuff */
            }
        })
        .setNegativeButton(R.string.btn_cancel, null)
       .create();
	}


	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		int v_id = v.getId();

		switch (v_id) {
		case R.id.return_btn:
			finish();
			break;
		case R.id.recharge:
			String amount = mAmountET.getText().toString();
			String cardValue = mCardValueET.getText().toString();
			String cardNo = mCardNoET.getText().toString();
			String password = mPasswordET.getText().toString();
			String cardType = (String)mCardTypeBT.getTag();
			
			String postUrl = "http://wap.qidian.cn/wap2/pay/sdlPay_yibaoOk.do?&sid=" + BookStoreActivity.user;
			StringBuffer sbuf = new StringBuffer();
			sbuf.append("mtd=sdlPay_19pay_payOk");
			sbuf.append("&cardType=");
			sbuf.append(cardType);
			sbuf.append("&amount=");
			sbuf.append(amount);
			sbuf.append("&totalAmount=");
			sbuf.append(cardValue);
			sbuf.append("&cardnum=");
			sbuf.append(cardNo);
			sbuf.append("&cardpwd=");
			sbuf.append(password);
			
			ALog.e("postUrl:"+postUrl);
			WebUtil.getHtml(this,this,postUrl,sbuf.toString());
			break;
		case R.id.card_type:
			showDialog(0);
			break;
		default:
			break;
		}
	}


	@Override
	public void onFinish(String html) {
		// TODO Auto-generated method stub
		ALog.e(html);
		ALog.e(html);
		if(html.equals("充值信息已提交! 请关注账号余额变化。如有问题，请联系客服")){
			ToastManager.getInstance().showText(this, "充值信息已提交! 请关注账号余额变化。如有问题，请联系客服。");
		}else{
			ToastManager.getInstance().showText(this, R.string.msg_submit_fail);
		}
	}


}